use clap::Parser;

use crate::{const_value::console_log, rtools};

#[derive(Parser, Debug)]
pub struct CommandsNew {
    /// 项目名称
    #[clap(name = "new_project_name")]
    project_name: String,

    /// 静默执行
    #[clap(short, long, default_value = "false")]
    quiet: bool,

    /// 从自定义模板构建
    #[clap(short, long, name = "template_name", default_value = "default")]
    template: String,
}

/// 创建新项目
pub fn run(config: CommandsNew) -> Result<(), std::io::Error> {
    let target_path = std::env::current_dir()?.join(&config.project_name);
    let mould_path = rtools::get_home_path().join("mould").join(&config.template);

    if let Ok(_) = target_path.read_dir() {
        if !config.quiet {
            console_log(&format!(
                "创建项目失败, 已存在名为{}的文件夹",
                config.project_name
            ));
        }
        return Ok(());
    }

    if !config.quiet {
        console_log(&format!("新建项目: {}", config.project_name));
        console_log(&format!("目标路径: {:?}", target_path));
        console_log(&format!("模板名称: {:?}", config.template));
    }

    // 从mould文件夹下获取模板并复制到目标路径
    rtools::copy_dir(&mould_path, &target_path)?;

    Ok(())
}
